package com.spt.core;

import java.sql.Connection;
import java.sql.DatabaseMetaData;
import java.sql.ResultSet;
import java.util.ArrayList;


import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.springframework.stereotype.Service;

import com.google.gson.Gson;
import com.spt.fjj.service.models.common.AnnotationCommonService;


@Service
public class SysTableInitImpl extends AnnotationCommonService implements ISysTableInit{

    private static Logger log = LogManager.getLogger("com.spt.ws.man");
    private static final Gson gson = new Gson();
/*    @Resource(name="sequenceTask")
    OracleSequenceMaxValueIncrementer sequence;*/
    @Override
    public void initSysTables() {
        /*sequence.nextIntValue();*/
        /*ResultSet tabs = null; 
        try {
            Connection conn = this.jdbcTemplate.getDataSource().getConnection();
            DatabaseMetaData dbMetaData = conn.getMetaData();
            String db_user = Property.getProperty("jdbc.user");
            String[]   types   =   { "TABLE" };
            tabs = dbMetaData.getTables(null, db_user.toUpperCase(), "t_shiro_role".toUpperCase(), types);
            if(!tabs.next()) {
                this._update(
                        "create table t_shiro_role " +
                                "( " + 
                                "  id         number, " + 
                                "  rolename   varchar2(30), " + 
                                "  roledesc   varchar2(100 char), " + 
                                "  createdt   date, " + 
                                "  creator    varchar2(30 char), " + 
                                "  lastupdate date " + 
                                ")");
                this._update("create sequence SEQ_SECURITY " + 
                        "minvalue 1 " + 
                        "maxvalue 99999999999999999999 " + 
                        "start with 100000" + 
                        "increment by 1 " + 
                        "cache 20 " + 
                        "cycle");
                this._update("insert into t_shiro_role values(0,'ADMIN','超级管理员，拥有所有权限',sysdate,'system',sysdate)");
                this._update("insert into t_shiro_role values(1,'USER','普通用户',sysdate,'system',sysdate)");
            }
            tabs.close();
            tabs = dbMetaData.getTables(null, db_user.toUpperCase(), "t_shiro_user".toUpperCase(), types);
            if(!tabs.next()) {
                this._update(
                        "create table t_shiro_user " + 
                        "( " + 
                        "  id         number, " + 
                        "  username   varchar2(30), " + 
                        "  nickname   varchar2(100 char), " + 
                        "  passwd     varchar2(100), " + 
                        "  describe       varchar2(100 char)," +
                        "  createdt   date, " + 
                        "  creator    varchar2(30 char), " + 
                        "  lastupdate date " + 
                        ")");
                this._update("insert into t_shiro_user values(0,'admin','','038bdaf98f2037b31f1e75b5b4c9b26e','超级管理员，拥有所有权限',sysdate,'system',sysdate)");
            }
            tabs.close();
            tabs = dbMetaData.getTables(null, db_user.toUpperCase(), "t_shiro_permission".toUpperCase(), types);
            if(!tabs.next()) {
                this._update(
                        "create table t_shiro_permission " + 
                        "( " + 
                        "  id         number, " + 
                        "  permission   varchar2(30), " + 
                        "  describe       varchar2(100 char)," +
                        "  createdt   date, " + 
                        "  creator    varchar2(30 char) " + 
                        ")");
            }
            tabs.close();
            tabs = dbMetaData.getTables(null, db_user.toUpperCase(), "t_shiro_user_role".toUpperCase(), types);
            if(!tabs.next()) {
                this._update(
                        "create table t_shiro_user_role " + 
                        "( " + 
                        "  user_id         number, " + 
                        "  role_id         number, " + 
                        "  createdt   date, " + 
                        "  creator    varchar2(30 char) " + 
                        ")");
                this._update("insert into t_shiro_user_role values(0,0,sysdate,'system')");
            }
            tabs.close();
            tabs = dbMetaData.getTables(null, db_user.toUpperCase(), "t_shiro_role_permission".toUpperCase(), types);
            if(!tabs.next()) {
                this._update(
                        "create table t_shiro_role_permission " + 
                        "( " + 
                        "  role_id         number, " + 
                        "  permission_id         number, " + 
                        "  createdt   date, " + 
                        "  creator    varchar2(30 char) " + 
                        ")");
            }
            tabs.close();
            tabs = dbMetaData.getTables(null, db_user.toUpperCase(), "t_TASK_INFO".toUpperCase(), types);
            if(!tabs.next()) {
                this._update(
                        "create table t_TASK_INFO" + 
                        "(" + 
                        "  taskid     VARCHAR2(20)," + 
                        "  groupid    VARCHAR2(20)," + 
                        "  jobname    VARCHAR2(100 CHAR)," + 
                        "  bean       VARCHAR2(20)," + 
                        "  cron       VARCHAR2(50)," + 
                        "  laststatus VARCHAR2(10)," + 
                        "  id         NUMBER" + 
                        ")");
                try {
                    this._update("create sequence SEQ_TASK_INFO " + 
                            "minvalue 1 " + 
                            "maxvalue 99999999999999999999 " + 
                            "start with 1000000 " + 
                            "increment by 1 " + 
                            "cache 20 " + 
                            "cycle");
                }catch (Exception e) {
                    e.printStackTrace();
                }
            }
            
        } catch (SQLException e) {
            
            e.printStackTrace();
        }*/
        
    }

    @Override
    public void initSysTableByConfigFile(){
        
        
        Connection conn = null;
        DatabaseMetaData dbMetaData = null;
        try {
            conn = this.jdbcTemplate.getDataSource().getConnection();
            dbMetaData = conn.getMetaData();
            
            String db_user = Property.getProperty("jdbc.user");
            String[]   types   =   { "TABLE" };
            
            String sequences = Property.getProperty("sequenctlist");
            if(sequences!=null) {
                String[] sequenceList = sequences.split(",");
                for(String seq : sequenceList ) {
                    String csql = Property.getProperty(seq);
                    try {
                        this._update(csql);
                    }catch(Exception e) {
                        log.error("建sequence出错，错误信息：" + e.getMessage());
                    }
                }
            }
            String tableListStr = Property.getProperty("tablelist");
            if(tableListStr != null) {
                String[] tableList = tableListStr.split(",");
                ResultSet tabs = null; 
                for(String table : tableList) {
                    tabs = dbMetaData.getTables(null, db_user.toUpperCase(), table.toUpperCase(), types);
                    if(!tabs.next()) {
                        String csql = Property.getProperty(table);
                        this._update(csql);
                        String insData = Property.getProperty(table + ".data");
                        if(insData != null) {
                            ArrayList<?> data = gson.fromJson(insData, ArrayList.class);
                            for (Object insSql : data) {
                                this._update((String) insSql);
                            }
                        }
                    }
                    tabs.close(); 
                }
            }
        } catch (Exception e) {
            log.error("初始化数据库表出错，错误信息：" + e.getMessage());
            return;
        }
    }

}
